package ru.urfc.portal.developer_tasks.ejb_tests;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ejb.EJB;

import ru.urfc.portal.dao.TmpContragentDao;
import ru.urfc.portal.developer_tasks.BaseTestEjb;
import ru.urfc.portal.model.Client;
import ru.urfc.portal.model.Branch;
import ru.urfc.portal.model.NotificationSettings;
import ru.urfc.portal.model.TmpContragent;
import ru.urfc.portal.model.accounting.Contragent;

public class TmpContragentDaoTestEjb extends BaseTestEjb {

	@EJB TmpContragentDao contragentDao;

	Contragent contragent = new Contragent(111222333L);
	Long operationId = 1L;
	Branch branchNorth;
	Branch branchSouth;

	@Override
	protected void setUp() throws Exception {
		branchNorth = exactlyOne(find(Branch.class, "code", Branch.SVERD.getCode()));
		branchSouth = exactlyOne(find(Branch.class, "code", Branch.KURGAN.getCode()));
	}

	public void testCreateTmpContragentNone() {
		contragentDao.createTmpContragents(operationId, branchNorth, new ArrayList<Contragent>());
	}

	public void testCreateTmpContragentWithNullInn() {
		List<TmpContragent> result = find(TmpContragent.class, "operationId", operationId);
		assertEquals(0, result.size());
		contragentDao.createTmpContragents(operationId, branchNorth, Arrays.asList(contragent));
		result = find(TmpContragent.class, "operationId", operationId);
		assertEquals(1, result.size());
		assertNull(result.get(0).getInn());
	}
	
	public void testCreateTmpContragentWithNotNullInn() {
		contragent.setInn("inn");
		contragentDao.createTmpContragents(operationId, branchNorth, Arrays.asList(contragent));
		List<TmpContragent> result = find(TmpContragent.class, "operationId", operationId);
		assertEquals(1, result.size());
		assertEquals("inn", result.get(0).getInn());
	}
	
	public void testUpdateTmpContragents() {
		Client client = persist(new Client(contragent, branchNorth, new NotificationSettings()));
		contragentDao.createTmpContragents(operationId, branchNorth, Arrays.asList(contragent));
		List<TmpContragent> result;
		result = find(TmpContragent.class, "operationId", operationId);
		assertEquals(1, result.size());
		assertNull(result.get(0).getClient());
		contragentDao.updateTmpContragents(operationId);
		result = find(TmpContragent.class, "operationId", operationId);
		assertEquals(1, result.size());
		assertEquals(client.getId(), result.get(0).getClient().getId());
	}
	
	public void testGenerateOperationId() {
		assertEquals(false, contragentDao.generateOperationId().equals(contragentDao.generateOperationId()));
	}
	
}
